package de.hmm.hwip.gui;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import de.hmm.hwip.gui.api.IRequest;
import de.hmm.hwip.gui.api.IResponse;
import de.hmm.hwip.osgi.view.ViewFactory;

public class hWipGui implements ViewFactory, IResponse{

	private IRequest service;
	private LinearLayout m_main;
	private Button open_button;
	private Button close_button;
	private Button test_button;
	private EditText m_text;
	private TextView m_result;
	private Activity activity;
	 

	public hWipGui(){
		System.out.println("hWip GUI created ...");
	}	
	 
	 @Override
	 public View create(final Activity activity) {
		 this.activity = activity;
		 
		 System.out.println("=== Create the view");
		 m_main = new LinearLayout(activity);
		 m_main.setOrientation(LinearLayout.VERTICAL);
        
		 m_text = new EditText(activity);
		 m_text.setLines(1);
		 
		 m_result = new TextView(activity);
		 
		 open_button = new Button(activity);
		 open_button.setText("openSocket");
		 open_button.setOnClickListener(new OnClickListener() {

			 public void onClick(View arg0) {
				 service.sendCommand("open");
			 }
            
		 });
		 
		 close_button = new Button(activity);
		 close_button.setText("closeSocket");
		 close_button.setOnClickListener(new OnClickListener() {

			 public void onClick(View arg0) {
				 service.sendCommand("close");
				 open_button.setEnabled(true);
			 }
            
		 });
		 
		 test_button = new Button(activity);
		 test_button.setText("temp test Button");
		 test_button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				service.sendCommand("rest");	
			}
		});
		 synchronized(this) {
			 if (service != null) {
				 close_button.setEnabled(true);
				 open_button.setEnabled(false);
			 } else {
				 open_button.setEnabled(false);
				 close_button.setEnabled(false);
				 m_result.setText("No Service bound");
			 }
		 }
     
		 m_result.setText("Hello, welcome to OSGi-Gui listener");
		 m_main.addView(m_text);
		 m_main.addView(close_button);
		 m_main.addView(open_button);
		 m_main.addView(test_button);
		 m_main.addView(m_result);
		 m_main.setVisibility(View.VISIBLE);
		 return m_main;
	 }
	 
	 public synchronized void bindIRequest(IRequest ir) {
	        service = ir;
	        if (activity != null) {
	        	/*activity.runOnUiThread(
	        	 * Runs the specified action on the UI thread. If the current thread is the 
	        	 * UI thread, then the action is executed immediately. If the current thread 
	        	 * is not the UI thread, the action is posted to the event queue of the UI thread.
	        	 * */
	            activity.runOnUiThread(new Runnable() {
	                public void run() {
	                    // Enable button
	                    open_button.setEnabled(false);
	                    close_button.setEnabled(true);
	                    m_result.setText("Ready to send Commands");
	                    m_main.invalidate();
	                }
	            });
	        }
	    }
	    
	    public synchronized void unbindIRequest(IRequest ic) {
	        service = null;
	        System.out.println("unbindIRequest");
	        if (activity != null) {
	        activity.runOnUiThread(new Runnable() {
	                public void run() {
	                    // Enable button
	                    open_button.setEnabled(false);
	                    close_button.setEnabled(false);
	                    m_result.setText("No Command-Service available");
	                    m_main.invalidate();
	                    System.out.println("=====> hWipGui was unbound from Command service");
	                }
	            });
	        }

	    }

		@Override
		public Activity getActivity() {
			return activity.getParent();
		}

		@Override
		public void responseText(final String response) {
			System.out.println("Gui gets response: " + response);
			activity.runOnUiThread(new Runnable() {
                public void run() {
                	m_result.setText(response);
                }
			});
			
		}

}
